package cn.hanna.springcloud.service.impl;

import cn.hanna.springcloud.service.StreamProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import org.springframework.cloud.stream.messaging.Source;
import java.util.UUID;

/**
 * @author smileha
 * @create 2021-10-19 23:07
 * @description
 */

/**
 * @Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})
 * @Retention(RetentionPolicy.RUNTIME)
 * @Documented
 * @Inherited
 * @Configuration -----------------------------注入容器了------------------------------------>
 *     @Target({ElementType.TYPE})
     * @Retention(RetentionPolicy.RUNTIME)
     * @Documented
     * @Component
 * @Import({BindingBeansRegistrar.class, BinderFactoryAutoConfiguration.class})
 * @EnableIntegration
 */
@EnableBinding(Source.class) // 此处spring发现是消息驱动，已经帮我们将其注入到容器中了
public class StreamProviderServiceImpl implements StreamProviderService {

    @Resource
    private MessageChannel output; // 获取到了管道对象

    @Override
    public String send() {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        output.send(MessageBuilder.withPayload(uuid).build()); // 利用消息构造器，包装消息内容，构成消息对象
        return uuid;
    }
}
